package com.xm.dao.background;

import com.xm.entity.Category;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 大类别
 * @author 康文程
 * @date 2024-1-20
 */
public class CategoryDao extends BaseUtil {
    /**
     * 根据 model_id 查询 category_name 和 category_id
     *
     * @param modelId 模型ID
     * @return String[] [category_name, category_id]
     * @throws SqlErrorException SQL执行异常
     */
    public List<Category> findCategoryByModelId(int modelId) throws SqlErrorException {
        @Language("SQL")
        String sql = "SELECT category_name, category_id FROM category WHERE model_id = ?";
        ResultSet rs =null;
        try {
            rs = executeQuery(sql, modelId);
            List<Category> categorieList = new ArrayList<>();
            while (rs.next()) {
                String categoryName = rs.getString("category_name");
                Integer categoryId = rs.getInt("category_id");
                categorieList.add(new Category(categoryId,categoryName));
            }
            return categorieList;
        } catch (SQLException e) {
            throw new SqlErrorException("根据 model_id 查询大类别失败", e);
        }finally {
            close(rs);
        }
    }
}
